# 0
# set操作谨防无限循环

# 1
# 属性被访问,请使用__getattribute__(this,name)

# 2

#  3,,2

class C:
    def __getattr__(self, item):
        print(1)

    def __getattribute__(self, item):
        print(2)

    def __setattr__(self, key, value):
        print(3)

    def __delattr__(self, item):
        print(4)


# c = C()
# c.x = 1
#
# print(c.x)


# 再次记住,没有返回值就默认返回None


# 3

# 2,,1

# 切记super对象没有__getattr__,也就是没有key值时候返回的对象

# 4
# 错误的
class Counter:
    def __init__(self):
        self.counter = 0

    def __setattr__(self, key, value):
        # self.counter += 1
        print("+1")

        # self.__dict__[key] = value +1  #正确的写法


        # super().__setattr__(key, value + 1)

    def __delattr__(self, item):
        self.counter -= 1
        super().__delattr__(item)


# 正确的??
class NewCounter:
    def __init__(self):
        super().__setattr__('counter', 0)

    def __setattr__(self, key, value):
        super().__setattr__('counter', self.counter + 1)
        super().__setattr__(key, value)

    def __delattr__(self, item):
        super().__setattr__('counter', self.counter - 1)
        super().__delattr__(item)


# c1 = Counter()
c2 = NewCounter()
c2.x = 2
c2.y = 3

# print(c2.counter)


# setattr(c1, 'name', 'gundam')
# print(c1.counter)

class Item:
    def __init__(self):
        self['number'] = 0

# item1 = Item()
# print(item1.number)

def hi2():
    print("hi45")